package com.chentf.spring.annotation.config;

import com.chentf.spring.annotation.bean.Car;
import com.chentf.spring.annotation.bean.Color;
import com.chentf.spring.annotation.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@ComponentScan({"com.chentf.spring.annotation.service", "com.chentf.spring.annotation.controller", "com.chentf.spring.annotation.dao", "com.chentf.spring.annotation.bean"})
public class MainConfigOfAutowired {

    // 我们需要注释掉BookService类中bookDao字段上的@Qualifier注解，这是因为@Qualifier注解为显示指定装配哪个组件，如果使用了@Qualifier注解，无论是否使用了@Primary注解，都会装配@Qualifier注解标注的对象。
    @Primary
    @Bean("bookDao2")
    public BookDao bookDao() {
        BookDao bookDao = new BookDao();
        bookDao.setLabel("2");
        return bookDao;
    }

    @Bean
    public Color color(@Autowired Car car) {
        Color color = new Color();
        color.setCar(car);
        return color;
    }
}
